﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  BaseXmlTest 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/10/16 21:55:00
 * Update Man   :  Joey Liang
 * Update Time  :  2013/10/16 21:55:00
************************************************************************/

using IdleTest.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using System.Xml.Fakes;

namespace IdleTest.Xml
{
    [TestClass]
     public abstract class BaseXmlTest
    {
        #region 初始化
        /// <summary>
        /// 
        /// </summary>
        protected XmlTestBuilder xmlBuilder;
        protected const string NODE_NAME = "ChildNode";
        protected const string NOT_NODE_NAME = "NotNode";
        protected const string ATTR_NAME = "attr";
        protected const string ATTR_INT_NAME = "attrint";
        protected const string NOT_ATTR_NAME = "notattr";
        protected const string ATTR_VALUE = "attrValue";
        protected const int ATTR_INT_VALUE = 22;
        protected const string NOT_ATTR_VALUE = "notattrValue";
        protected const string DEFAULT_XPATH = "/root/" + NODE_NAME;
        protected const string NOTNODE_XPATH = "/root/" + NOT_NODE_NAME;

        [TestInitialize]
        public void init()
        {
            string xml = string.Format(@"<root>
                                <{0} {2}='{3}' {4}='{5}' />
                                <{0} {2}='{6}' {4}='{5}' />
                                <{0} {7}='{3}' {4}='{5}' />
                                <{1} {2}='{3}' {4}='{5}' />
                                <{1} {2}='{3}' {4}='' />
                            </root>"
                , NODE_NAME, NOT_NODE_NAME, ATTR_NAME, ATTR_VALUE, ATTR_INT_NAME, ATTR_INT_VALUE, NOT_ATTR_VALUE, NOT_ATTR_NAME);
            this.xmlBuilder = new XmlTestBuilder(xml);

            AssertCommon.ResetAssertInsance(new AssertXml());
        }
        #endregion

        protected Func<XmlNode, bool> trueFunc
        {
            get
            {
                return p => p != null &&
                    p.Attributes[ATTR_NAME] != null && p.Attributes[ATTR_NAME].Value == ATTR_VALUE;
            }
        }

        protected Func<XmlNode, bool> falseFunc
        {
            get
            {
                return p => p != null &&
                    p.Attributes[ATTR_NAME] != null && p.Attributes[ATTR_NAME].Value == "aaaa";
            }
        }

        protected StubXmlNodeList GetStubXmlNodeList(params XmlNode[] node)
        {
            StubXmlNodeList nodelist = new StubXmlNodeList();
            nodelist.GetEnumerator01 = () => { return XmlTestBuilder.GetXmlNodeList(node); };
            return nodelist;
        }

        protected XmlNodeList GetExpectedTureNodes()
        {
            return xmlBuilder.GetNodeList(string.Format("{0}[@{1}='{2}']", DEFAULT_XPATH, ATTR_NAME, ATTR_VALUE));
        }

        protected XmlNode GetExpectedTureNode()
        {
            return xmlBuilder.GetNode(string.Format("{0}[@{1}='{2}']", DEFAULT_XPATH, ATTR_NAME, ATTR_VALUE));
        }

        protected XmlNode GetAttrValueNullNode()
        {
            return xmlBuilder.GetNode(string.Format("{0}[@{1}='{2}']", NOTNODE_XPATH, ATTR_INT_NAME, string.Empty));
        }
    }
}
